<?php

namespace SDK;

use SDK\Client;
use SDK\BuildClient;
use Constants\StatusCode;
use Exception;

require_once 'sdk/common.php';
require_once 'constants/constants.php';

class Charge extends Client {
    public $client;
    public $app_id;
    public $app_secret;
    public $base_url;

    public function __construct(Client $client) {
        $this->client = $client;
        $this->app_id = $client->app_id;
        $this->app_secret = $client->app_secret;
        $this->base_url = $client->base_url;
    }

    public function chargeStart($req) {
        $commonMethod = new BuildClient($this->app_id,$this->app_secret); 
        $postParams = $commonMethod->build_post_param($req);
        $response = $this->client->post( '/open/charge/start', post_params:  $postParams);
        return $this->handleResponse(response: $response);
    }

    public function chargeStop($req) {
        $commonMethod = new BuildClient($this->app_id,$this->app_secret); 
        $postParams = $commonMethod->build_post_param($req);
        $response = $this->client->post('/open/charge/stop',  $postParams,
        );
        return $this->handleResponse($response);
    }

    private function handleResponse($response) {
        $jsonString = json_encode($response);
        $data = json_decode($jsonString,true);
        if ($data['code'] !== StatusCode::SucceedCode) {
            throw new Exception($data['message']);
        }
        return $data;
    }
}